<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryForm"
      :inline="true"
      v-show="showSearch"
      label-width="100px"
    >
      <el-form-item label="原编码" prop="cdCode">
        <el-input
          v-model="queryParams.cdCode"
          placeholder="请输入原编码"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="监测区名称" prop="areaName">
        <el-input
          v-model="queryParams.areaName"
          placeholder="请输入监测区名称"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="采样长度" prop="cyLength">
        <el-input
          v-model="queryParams.cyLength"
          placeholder="请输入采样长度"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="安装方式" prop="installationMethod">
        <el-input
          v-model="queryParams.installationMethod"
          placeholder="请输入安装方式"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button
          type="primary"
          icon="el-icon-search"
          size="mini"
          @click="handleQuery"
          >搜索</el-button
        >
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
          >重置</el-button
        >
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['define:quakeDefine:add']"
          >新增
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['define:quakeDefine:edit']"
          >修改
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['define:quakeDefine:remove']"
          >删除
        </el-button>
      </el-col>
      <right-toolbar
        :showSearch.sync="showSearch"
        @queryTable="getList"
      ></right-toolbar>
    </el-row>

    <el-table
      v-loading="loading"
      :data="quakeDefineList"
      @selection-change="handleSelectionChange"
    >
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="序号" width="80" align="center">
        <template slot-scope="scope">
          <span style="margin-left: 10px">{{ scope.$index + 1 }}</span>
        </template>
      </el-table-column>
      <el-table-column label="原编码" align="center" prop="cdCode"  width="150"/>
      <el-table-column label="测点编码" align="center" prop="remark"  width="150"/>
      <el-table-column label="监测区名称" align="center" prop="areaName"  width="150"/>
      <el-table-column
        label="记录通道数"
        align="center"
        prop="recordChannelNumber"
         width="150"
      />
      <el-table-column label="采样长度" align="center" prop="cyLength"  width="150">
        <template slot-scope="scope">
          <span>{{ scope.row.cyLength }} <span class="price">s</span></span>
        </template>
      </el-table-column>
      <el-table-column label="采样频率" align="center" prop="cyFrequency"  width="150">
        <template slot-scope="scope">
          <span>{{ scope.row.cyFrequency }} <span class="price">Hz</span></span>
        </template>
      </el-table-column>
      <el-table-column
        label="传感器方向"
        align="center"
        prop="sensorDirection"
         width="150"
      />
      <el-table-column
        label="安装方式"
        align="center"
        prop="installationMethod"
         width="150"
      />
      <el-table-column label="灵敏度" align="center" prop="sensitivity"  width="150">
        <template slot-scope="scope">
          <span
            >{{ scope.row.sensitivity }}
            <span class="price">V/(m/s)</span></span
          >
        </template>
      </el-table-column>
      <el-table-column label="传感器类型" align="center" prop="sensorType"  width="150"/>
      <el-table-column label="传感器位置" align="center" prop="sensorAddress"  width="150"/>
      <el-table-column label="坐标X" align="center" prop="wzX"  width="150"/>
      <el-table-column label="坐标Y" align="center" prop="wzY"  width="150"/>
      <el-table-column label="坐标Z" align="center" prop="wzZ"  width="150"/>
      <el-table-column
        label="安装时间"
        align="center"
        prop="azTime"
        width="180"
      >
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.azTime, "{y}-{m}-{d}") }}</span>
        </template>
      </el-table-column>

      <el-table-column
        label="操作"
        align="center"
        class-name="small-padding fixed-width"
         width="150"
         fixed="right"
      >
        <template slot-scope="scope">
          <el-button-group>
            <el-button
              size="small"
              type="success"
              icon="el-icon-edit"
              @click="handleUpdate(scope.row)"
              v-hasPermi="['define:quakeDefine:edit']"
            >
            </el-button>
            <el-button
              size="small"
              type="danger"
              icon="el-icon-delete"
              @click="handleDelete(scope.row)"
              v-hasPermi="['define:quakeDefine:remove']"
            >
            </el-button>
          </el-button-group>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改微震监测基础信息对话框 -->
    <el-dialog
      :title="title"
      :visible.sync="open"
      width="1000px"
      append-to-body
    >
      <el-form ref="form" :model="form" :rules="rules" label-width="100px">
        <el-row>
          <el-col :span="12">
            <el-form-item label="原编码" prop="cdCode">
              <el-input v-model="form.cdCode" placeholder="请输入原编码" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="监测区名称" prop="areaName">
              <el-input
                v-model="form.areaName"
                placeholder="请输入监测区名称"
              />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="记录通道数" prop="recordChannelNumber">
              <el-input
                v-model="form.recordChannelNumber"
                placeholder="请输入记录通道数"
              />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="采样长度" prop="cyLength">
              <el-input
                v-model="form.cyLength"
                placeholder="请输入采样长度"
                style="width:80%"
              />
              <span class="unit">s</span>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="采样频率" prop="cyFrequency">
              <el-input
                v-model="form.cyFrequency"
                placeholder="请输入采样频率"
                style="width:80%"
              />
              <span class="unit">Hz</span>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="传感器方向" prop="sensorDirection">
              <el-input
                v-model="form.sensorDirection"
                placeholder="请输入传感器方向"
              />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="安装方式" prop="installationMethod">
              <el-input
                v-model="form.installationMethod"
                placeholder="请输入安装方式"
              />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="灵敏度" prop="sensitivity">
              <el-input
                v-model="form.sensitivity"
                placeholder="请输入灵敏度"
                @input="inputnum"
                style="width:80%"
              />
              <span class="unit">V/(m/s)</span>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="传感器类型" prop="sensorType">
              <el-select
                v-model="form.sensorType"
                placeholder="请选择传感器类型"
                style="width:100%"
              >
                <el-option
                  v-for="dict in sensorTypeList"
                  :key="dict.sensorTypeCode"
                  :label="dict.sensorTypeDesc"
                  :value="dict.sensorTypeCode"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="传感器位置" prop="sensorAddress">
              <el-input
                v-model="form.sensorAddress"
                placeholder="请输入传感器位置"
              />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="坐标X" prop="wzX">
              <el-input v-model="form.wzX" placeholder="请输入坐标X" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="坐标Y" prop="wzY">
              <el-input v-model="form.wzY" placeholder="请输入坐标Y" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="坐标Z" prop="wzZ">
              <el-input v-model="form.wzZ" placeholder="请输入坐标Z" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="安装时间" prop="azTime">
              <el-date-picker
                clearable
                size="small"
                v-model="form.azTime"
                type="date"
                value-format="yyyy-MM-dd"
                placeholder="选择安装时间"
                style="width:100%;"
              >
              </el-date-picker>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  listQuakeDefine,
  getQuakeDefine,
  delQuakeDefine,
  addQuakeDefine,
  updateQuakeDefine
} from "@/api/define/quakeDefine";
import { listSensorTypeData } from "@/api/dictData/sensorTypeData";
export default {
  name: "QuakeDefine",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 微震监测基础信息表格数据
      quakeDefineList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 安装时间时间范围
      daterangeAzTime: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        mineCode: null,

        mineName: null,

        cdCode: null,

        areaName: null,

        recordChannelNumber: null,

        cyLength: null,

        cyFrequency: null,

        sensorDirection: null,

        installationMethod: null,

        sensitivity: null,

        sensorType: null,

        sensorAddress: null,

        wzX: null,

        wzY: null,

        wzZ: null,

        azTime: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        cdCode: [
          { required: true, message: "原编码不能为空", trigger: "blur" },
          { min: 1, max: 8, message: "长度在 1 到 8 个字符", trigger: "blur" }
        ],

        areaName: [
          { required: true, message: "监测区名称不能为空", trigger: "blur" }
        ],

        recordChannelNumber: [
          { required: true, message: "记录通道数不能为空", trigger: "blur" },
          { min: 1, max: 4, message: "长度在 1 到 4 个字符", trigger: "blur" }
        ],

        cyLength: [
          { required: true, message: "采样长度不能为空", trigger: "blur" },
          { min: 1, max: 4, message: "长度在 1 到 4 个字符", trigger: "blur" }
        ],

        cyFrequency: [
          { required: true, message: "采样频率不能为空", trigger: "blur" },
          { min: 1, max: 4, message: "长度在 1 到 4 个字符", trigger: "blur" }
        ],

        sensorDirection: [
          { required: true, message: "传感器方向不能为空", trigger: "blur" },
          { min: 1, max: 10, message: "长度在 1 到 10 个字符", trigger: "blur" }
        ],

        installationMethod: [
          { required: true, message: "安装方式不能为空", trigger: "blur" },
          { min: 1, max: 20, message: "长度在 1 到 20 个字符", trigger: "blur" }
        ],

        sensitivity: [
          { required: true, message: "灵敏度不能为空", trigger: "blur" },
          {
            min: 1,
            max: 8,
            message: "长度在 1 到 8 个字符,小数点后面2位",
            trigger: "blur"
          }
        ],

        sensorType: [
          { required: true, message: "传感器类型不能为空", trigger: "blur" }
        ],

        sensorAddress: [
          { required: true, message: "传感器位置不能为空", trigger: "blur" }
        ],

        wzX: [{ required: true, message: "坐标X不能为空", trigger: "blur" }],

        wzY: [{ required: true, message: "坐标Y不能为空", trigger: "blur" }],

        wzZ: [{ required: true, message: "坐标Z不能为空", trigger: "blur" }],

        azTime: [
          { required: true, message: "安装时间不能为空", trigger: "blur" }
        ]
      },
      //传感器类型
      sensorTypeList: [],
      TypeParams: {
        pageNum: 1,
        pageSize: 100
      }
    };
  },
  created() {
    this.getList();
    listSensorTypeData(this.TypeParams).then(response => {
      this.sensorTypeList = response.rows;
    });
  },
  methods: {
    // 限制只能在输入框中输入小数点后两位
    inputnum() {
      this.form.sensitivity = this.form.sensitivity.replace(
        /^(\-)*(\d+)\.(\d\d).*$/,
        "$1$2.$3"
      );
    },
    /** 查询微震监测基础信息列表 */
    getList() {
      this.loading = true;
      this.queryParams.params = {};
      if (null != this.daterangeAzTime && "" != this.daterangeAzTime) {
        this.queryParams.params["beginAzTime"] = this.daterangeAzTime[0];
        this.queryParams.params["endAzTime"] = this.daterangeAzTime[1];
      }
      listQuakeDefine(this.queryParams).then(response => {
        this.quakeDefineList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,

        mineCode: null,

        mineName: null,

        cdCode: null,

        areaName: null,

        recordChannelNumber: null,

        cyLength: null,

        cyFrequency: null,

        sensorDirection: null,

        installationMethod: null,

        sensitivity: null,

        sensorType: null,

        sensorAddress: null,

        wzX: null,

        wzY: null,

        wzZ: null,

        azTime: null,

        createBy: null,

        createTime: null,

        updateBy: null,

        updateTime: null,

        remark: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.daterangeAzTime = [];
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加微震监测基础信息";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids;
      getQuakeDefine(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改微震监测基础信息";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateQuakeDefine(this.form).then(response => {
              this.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addQuakeDefine(this.form).then(response => {
              this.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$confirm("是否确认删除当前选中数据？", "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      })
        .then(function() {
          return delQuakeDefine(ids);
        })
        .then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        })
        .catch(() => {});
    }
  }
};
</script>
